<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\member\controller;

/** 控制器类引入区 **/

use app\common\controller\Alisms;
use app\common\controller\Smscm;

/** 模型类引入区 **/

/** 核心或第三方类引入区 **/

use think\facade\Hook;
use think\Db;
use think\Validate;
use think\facade\Env;
use think\Request;


/**
 * 前台首页控制器
 * @package app\api\controller
 */
class Common extends Base
{
    public $noNeedLogin = ['*'];

    /**
     * 获取门店二维码
     */
    public function createRoomCode()
    {

        $store_id = $this->request->post('store_id', 0);
        if (empty($store_id)) {
            $this->result('门店id不能为空', '', 204);
        }
        $url = config('site_url') . '/store/#/' . '?store_id=' . $store_id;
        $path = 'uploads/store_code/';
        $filename = 'store_code_' . $store_id . '.png';
        $result = create_ewm($url, $path, $filename, 6);
        $img_path = getUrl() . '/' . $result;
        $data['store_id'] = $store_id;
        $data['img_path'] = $img_path;
        $this->result('查询成功', $data, 200);
    }

    /**
     *  获取  关于我们 信息
     **/
    public function getAbout()
    {
        $row = [
            'site_logo' => get_img_path(config('site_logo')),      //LOGO
            'current_version' => config('current_version'),                    //当前版本
            'web_site_url' => config('web_site_url'),              //官网网址
            'off_wx' => config('off_wx'),                    //官方微信
            'mailbox' => config('mailbox'),                   //官方邮箱
            'cust_phone' => config('cust_phone'),                 //客服电话
            'cust_wx_code' => get_img_path(config('cust_wx_code')),   //客服微信二维码
            'cust_wx' => config('cust_wx'),   //客服微信号
        ];
        $this->result('获取成功', $row, 200);
    }


    /**
     *  地区 三级联动
     **/
    public function getAreaLinkage()
    {
        $pid = $this->request->post('pid', 0);
        $list = areaLinkage($pid);
        $this->result('获取成功', $list, 200);
    }

    /**
     *  获取 协议 富文本
     **/
    public function getagreement()
    {
        $row = [
            'service_content' => check_textarea_img(config('service_content')),
            'privacy_content' => check_textarea_img(config('privacy_content')),
        ];
        $this->result('获取成功', $row, 200);
    }

    /**
     *  上传 图片
     **/
    public function doupload()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (empty($data['imgdata'])) {
                $this->result('请上传图片', '', 204);
            }
            $bg = $data['imgdata'];//获取图片流
            $image_content = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $bg));
            $filename = time() . rand('1111', '9999') . '.jpg';
            $path = 'uploads/images/' . date('Ymd');
            $dir = $path . '/' . $filename;
            if (!is_dir($path)) {
                mkdir($path);
            }
            file_put_contents($dir, $image_content);
            $imgurl = 'uploads/images/' . date('Ymd') . '/' . $filename;
            $data1 = array(
                'path' => $imgurl,
                'name' => $filename,
                'module' => 'index',
                'md5' => md5($filename),
                'thumb' => $imgurl,
                'url' => '1',
                'siza' => '0',
                'mime' => $filename,
                'ext' => '0',
                'sha1' => sha1($filename),
                'create_time' => time(),
                'update_time' => time(),
            );
            $re = Db::name('admin_attachment')->insertGetId($data1);
            $attac = get_img_path($re);
            $data = array(
                'imgurl' => $attac,
                'img_id' => $re,
            );
            $this->result('上传成功', $data, 200);
        } else {
            $this->result('error', '', 204);
        }
    }

    /**
     *  上传 图片 (文件流)
     **/
    public function uploadImage()
    {
        if ($this->request->isPost()) {
            $file = $this->request->file('file');
            if (!$file) {
                $this->result('请上传图片', '', 204);
            }

            $filename = time() . rand('1111', '9999') . '.jpg';
            $path = 'uploads/images/' . date('Ymd');
            if (!is_dir($path)) {
                mkdir($path);
            }
            // 移动到框架应用根目录/uploads/ 目录下
            $info = $file->move($path, $filename);
            if ($info) {
                $imgurl = 'uploads/images/' . date('Ymd') . '/' . $filename;
                $data1 = array(
                    'path' => $imgurl,
                    'name' => $filename,
                    'module' => 'index',
                    'md5' => md5($filename),
                    'thumb' => $imgurl,
                    'url' => '1',
                    'siza' => '0',
                    'mime' => $filename,
                    'ext' => 'png',
                    'sha1' => sha1($filename),
                    'create_time' => time(),
                    'update_time' => time(),
                );
                $re = Db::name('admin_attachment')->insertGetId($data1);
                $attac = get_img_path($re);
                $data = array(
                    'imgurl' => $attac,
                    'img_id' => $re,
                );
                $this->result('上传成功', $data, 200);
            } else {
                $this->result('上传失败', '', 204);
            }

        } else {
            $this->result('error', '', 204);
        }
    }


}
